package com.example.supermarket.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.supermarket.PageModel;
import com.example.supermarket.Result;
import com.example.supermarket.entity.Category;
import com.example.supermarket.entity.Employee;
import com.example.supermarket.entity.Goods;
import com.example.supermarket.service.CategoryService;
import com.example.supermarket.service.DepartmentService;
import com.example.supermarket.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hpp
 * @since 2021-10-26
 */
@RestController
@RequestMapping("/goods")
@CrossOrigin
public class GoodsController {
    @Autowired
    GoodsService goodsService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    DepartmentService departmentService;
    /*添加商品*/
    @PostMapping("")
    public Result<Goods> addGoods(@RequestParam("gdName") String gdName,
                                  @RequestParam("gdPrice") Double gdPrice,
                                  @RequestParam("gdIntroduce") String gdIntroduce,
                                  @RequestParam("cateId") Integer cateId,
                                  @RequestParam("depId") Integer depId){
        Goods goods = new Goods();
        goods.setGdName(gdName);
        goods.setGdPrice(gdPrice);
        goods.setGdIntroduce(gdIntroduce);
        goods.setCateId(cateId);
        goods.setDepId(depId);
        boolean save = goodsService.save(goods);
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("gd_name",gdName);
        if (save) {
            Goods one = goodsService.getOne(queryWrapper);
            return new Result<Goods>(200,"ok",one);
        }
        return new Result<Goods>(0,"error",null);
    }
    /*删除商品*/
    @DeleteMapping("/{id}")
    public Result<Goods> deleteGoods(@PathVariable Integer id){
        boolean b = goodsService.removeById(id);
        if (b) {
            return new Result<Goods>(200,"ok",null);
        }
        return new Result<Goods>(0,"error",null);
    }
    /*修改商品信息*/
    @PutMapping("")
    public Result<Goods> modifyGoods(@RequestParam("gdId") Integer gdId,
                                     @RequestParam("gdName") String gdName,
                                     @RequestParam("gdPrice") Double gdPrice,
                                     @RequestParam(value="gdCount",required = false,defaultValue = "0") Integer gdCount,
                                     @RequestParam(value="gdIntroduce",required = false,defaultValue = "") String gdIntroduce,
                                     @RequestParam("cateId") Integer cateId,
                                     @RequestParam("depId") Integer depId){
        UpdateWrapper<Goods> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("gd_id",gdId)
                .set("gd_name",gdName)
                .set("gd_price",gdPrice)
                .set("gd_count",gdCount)
                .set("gd_introduce",gdIntroduce)
                .set("cate_id",cateId)
                .set("dep_id",depId);

        boolean update = goodsService.update(updateWrapper);

        Goods byId = goodsService.getById(gdId);

        if (update) {
            return new Result<Goods>(200,"ok",byId);
        }
        return new Result<Goods>(0,"error",null);
    }
    /*分页查询所有商品列表,支持搜索功能,若查询参数为空，表示查询所有商品*/
    @GetMapping("")
    public Result<PageModel<Goods>> getGoodsList(@RequestParam (value = "query",required = false,defaultValue = "") String query,
                                                 @RequestParam("pagenum") Integer pagenum,
                                                 @RequestParam("pagesize") Integer pagesize){
        IPage<Goods> ipage = new Page<>(pagenum,pagesize);
        System.out.println("pagesize:" + pagesize);
        System.out.println("query:"+ query);
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("gd_name",query);
        IPage<Goods> page  = goodsService.page(ipage,queryWrapper);
        List<Goods> records = page.getRecords();
        for (Goods record : records) {
            Integer cateId = record.getCateId();
            record.setCateName(categoryService.getById(cateId).getCateName());
            Integer depId = record.getDepId();
            record.setDepName(departmentService.getById(depId).getDepName());
        }
        PageModel<Goods> goodsResult = new PageModel<>(page.getTotal(),page.getCurrent(),page.getRecords());
        return new Result<>(200,"ok",goodsResult);
    }
    /*根据id获取商品*/
    @GetMapping("/{id}")
    public Result<Goods> getGoodsById(@PathVariable Integer id){
        Goods byId = goodsService.getById(id);
        if (byId != null) {
            return new Result<Goods>(200,"ok",byId);
        }
        return new Result<Goods>(0,"error",null);
    }
    /*根据{depId/cateId}路径获取指定的商品列表*/
    @GetMapping("/{depId}/{cateId}")
    public Result<PageModel<Goods>> getSpecialGoodsList(@PathVariable Integer depId,
                                                   @PathVariable Integer cateId,
                                                   @RequestParam("pagenum") Integer pagenum,
                                                   @RequestParam("pagesize") Integer pagesize){
        IPage<Goods> ipage = new Page<>(pagenum,pagesize);
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dep_id",depId)
                    .eq("cate_id",cateId);
        IPage<Goods> page  = goodsService.page(ipage,queryWrapper);
        PageModel<Goods> goodsResult = new PageModel<>(page.getTotal(),page.getCurrent(),page.getRecords());
        return new Result<>(200,"ok",goodsResult);
    }
}

